//
//  MBSpaceNewsCell.m
//  MiBaoTu
//
//  Created by admin on 16/9/20.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceNewsCell.h"

@interface MBSpaceNewsCell () {
    UIView                      *_bgView;
    UIButton                    *_selectBt;
    UIView                      *_contentBigView;
    UIImageView                 *_userImgView;
    UILabel                     *_nickNameLb;
    UILabel                     *_pinlun;
    UILabel                     *_timeLb;
    UILabel                     *_commentLb;
    UIView                      *_contentView;
    UIImageView                 *_contentImgView;
    UILabel                     *_contentNameLb;
    UILabel                     *_replyLb;
    UIButton                    *_replyBt;
}

@property (nonatomic,assign)BOOL isCompliling;   //是否编译
@property (nonatomic,assign)BOOL isALlSelecting; //是否全选

@end
@implementation MBSpaceNewsCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    //勾选按钮
    _selectBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectBt.backgroundColor = [UIColor clearColor];
    _selectBt.hidden = YES;
    [_selectBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [_selectBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateHighlighted];
    [_selectBt addTarget:self action:@selector(onGouXuanToSelectAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_selectBt];
    
    //**************************
    //大模块 包含音乐标题、大小、类型以及下载图标
    _contentBigView = [[UIView alloc]initWithFrame:CGRectZero];
    _contentBigView.backgroundColor = [UIColor whiteColor];
    [_bgView addSubview:_contentBigView];
    
    //头像
    _userImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _userImgView.backgroundColor = [UIColor clearColor];
    [_contentBigView addSubview:_userImgView];
    
    //昵称
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:13.f];
    [_contentBigView addSubview:_nickNameLb];
    
    //评论
    _pinlun = [[UILabel alloc]initWithFrame:CGRectZero];
    _pinlun.backgroundColor = [UIColor clearColor];
    _pinlun.textAlignment = NSTextAlignmentLeft;
    _pinlun.textColor = UIColorFromHex(0x999999);
    _pinlun.font = [UIFont systemFontOfSize:12.f];
    _pinlun.text = @"评论";
    [_contentBigView addSubview:_pinlun];

    //时间
    _timeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _timeLb.backgroundColor = [UIColor clearColor];
    _timeLb.textAlignment = NSTextAlignmentLeft;
    _timeLb.textColor = UIColorFromHex(0x999999);
    _timeLb.font = [UIFont systemFontOfSize:11.f];
    [_contentBigView addSubview:_timeLb];
    
    //评论内容
    _commentLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _commentLb.backgroundColor = [UIColor clearColor];
    _commentLb.textAlignment = NSTextAlignmentLeft;
    _commentLb.textColor = UIColorFromHex(0x333333);
    _commentLb.font = [UIFont systemFontOfSize:13.f];
    [_contentBigView addSubview:_commentLb];
    
    //内容
    _contentView = [[UIView alloc]initWithFrame:CGRectZero];
    _contentView.backgroundColor = UIColorFromHex(0xeeeeee);
    [_contentBigView addSubview:_contentView];
    
    //内容头像
    _contentImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _contentImgView.backgroundColor = [UIColor clearColor];
    [_contentView addSubview:_contentImgView];
    
    //内容标题
    _contentNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _contentNameLb.backgroundColor = [UIColor clearColor];
    _contentNameLb.textAlignment = NSTextAlignmentLeft;
    _contentNameLb.textColor = UIColorFromHex(0x333333);
    _contentNameLb.font = [UIFont systemFontOfSize:13.f];
    [_contentView addSubview:_contentNameLb];
    
    //回复
    _replyLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _replyLb.backgroundColor = [UIColor clearColor];
    _replyLb.textAlignment = NSTextAlignmentLeft;
    _replyLb.textColor = UIColorFromHex(0x333333);
    _replyLb.font = [UIFont systemFontOfSize:13.f];
    _replyLb.hidden = YES;
    [_contentBigView addSubview:_replyLb];

    _replyBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [_replyBt setTitle:@"回复" forState:UIControlStateNormal];
    [_replyBt setTitle:@"回复" forState:UIControlStateHighlighted];
    [_replyBt setTitleColor:TINK_BG_COLOR forState:UIControlStateNormal];
    [_replyBt setTitleColor:TINK_BG_COLOR forState:UIControlStateHighlighted];
    _replyBt.titleLabel.font = _nickNameLb.font;
    [_replyBt setTitleEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
    [_replyBt setImage:[UIImage imageNamed:@"huifu_pl"] forState:UIControlStateNormal];
    [_replyBt setImage:[UIImage imageNamed:@"huifu_pl"] forState:UIControlStateHighlighted];
    [_replyBt setImageEdgeInsets:UIEdgeInsetsMake(0, (WIDTH_5S?20:10), 0, 0)];
    _replyBt.backgroundColor = [UIColor clearColor];
    [_replyBt addTarget:self action:@selector(onSelectReplyAction) forControlEvents:UIControlEventTouchUpInside];
    [_contentBigView addSubview:_replyBt];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

//勾选
- (void)onGouXuanToSelectAction:(UIButton *)bt
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectControlForGouXuanWithValue:WithSelect:WithView:)]) {
        bt.selected = !bt.selected;
        if (_dataDic) {
            [_delegate onSelectControlForGouXuanWithValue:_dataDic WithSelect:bt.selected WithView:self];
        }
    }
}

//回复
- (void)onSelectReplyAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectControlForReplyWithValue:withView:)]) {
        [_delegate onSelectControlForReplyWithValue:_dataDic withView:self];
    }
}

- (void)onCompileCellForLayout:(BOOL)isCompile WithAllSelct:(BOOL)isAllSelct
{
    self.isCompliling = isCompile;
    self.isALlSelecting = isAllSelct;
    [self setNeedsLayout];
}

- (void)setIsCompliling:(BOOL)isCompliling
{
    _isCompliling = isCompliling;
    [self setNeedsLayout];
}

- (void)setIsALlSelecting:(BOOL)isALlSelecting
{
    _isALlSelecting = isALlSelecting;
    [self setNeedsLayout];
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    [self setNeedsLayout];
}
#pragma make---转码
- (NSString *)replaceUnicode:(NSString *)unicodeStr {
    //张三  \u5f20\u4e09
    
    NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
                                                           mutabilityOption:NSPropertyListImmutable
                                                                     format:NULL
                                                           errorDescription:NULL];
    
    //  NSLog(@"Output = %@", returnStr);
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    //value
    if (!_dataDic) return;
    UIImage *selectNoImg = [UIImage imageNamed:@"weigouxuan_fb"];
    UIImage *selectImg = [UIImage imageNamed:@"xuanzhong_zl"];
    [_selectBt setImage:selectNoImg forState:UIControlStateNormal];
    [_selectBt setImage:selectImg forState:UIControlStateSelected];
    _selectBt.selected = self.isALlSelecting;
    
    //头像
    NSString *FaceImg = [_dataDic objectForKey:@"FaceImg"];
    if ([FaceImg isKindOfClass:[NSNull class]]) {
        _userImgView.image = [UIImage imageNamed:@"story_header_bg"];
    } else {
        if (![FaceImg hasPrefix:@"http"]) {
            FaceImg = [NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,FaceImg];
        }
        [_userImgView sd_setImageWithURL:[NSURL URLWithString:FaceImg] placeholderImage:[UIImage imageNamed:@"story_header_bg"]];
    }
    
    //昵称
    _nickNameLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:(_isHide == YES ? @"NickName" : @"ReplyName")]];
    
    //时间
    _timeLb.text = [[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"CreateTime"]]getChaTime];
    
   // _commentLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"Content"]];
     _commentLb.text = [self replaceUnicode:[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"Content"]]];
    if ([_dataDic.allKeys containsObject:@"CommentType"]) {
        if (![[_dataDic objectForKey:@"CommentType"] isKindOfClass:[NSNull class]]) {
            if ([[_dataDic objectForKey:@"CommentType"]intValue] == 1) {
                //亲子秀
                _contentImgView.hidden = NO;
                NSString *CoverPath = [_dataDic objectForKey:@"CoverPath"];
                if ([CoverPath isKindOfClass:[NSNull class]]) {
                    _contentImgView.image = [UIImage imageNamed:@"qzx_player_bg"];
                } else {
                    if (![CoverPath hasPrefix:@"http"]) {
                        CoverPath = [NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,CoverPath];
                    }
                    
                    [_contentImgView sd_setImageWithURL:[NSURL URLWithString:CoverPath] placeholderImage:[UIImage imageNamed:@"qzx_player_bg"]];
                }
                
                //内容标题
                _contentNameLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"ShowTitle"]];
            }
            else if ([[_dataDic objectForKey:@"CommentType"]intValue] == 2) {
                //帖子
                 _contentImgView.hidden = YES;
                
                //内容标题
                _contentNameLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"ForumTitle"]];
            }
        }
    }

    _replyLb.hidden = _isHide;
    _replyLb.text = [self replaceUnicode:[NSString stringWithFormat:@"回复:%@",[_dataDic objectForKey:@"ReplyCont"]]];
    _replyBt.hidden = NO;//!_replyLb.hidden;
    
    //frame
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, (_replyLb.isHidden ? 246*PERCEN : 246*PERCEN+20));
    _selectBt.frame = CGRectMake((ISIPAD ?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (_bgView.height-selectNoImg.size.height)/2., selectNoImg.size.width, selectNoImg.size.height);
    _selectBt.hidden = !_isCompliling;
    
    _contentBigView.frame = CGRectMake((_selectBt.isHidden ? 0 : _selectBt.right+5), 0, _bgView.width, _bgView.height);
    
    _userImgView.frame = CGRectMake((ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), 20*PERCEN, 65*PERCEN, 65*PERCEN);
    _userImgView.layer.cornerRadius = _userImgView.height / 2.;
    _userImgView.layer.masksToBounds = YES;
    
    float nickWidth = [_nickNameLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_nickNameLb.font} context:NULL].size.width+6;
    _nickNameLb.frame = CGRectMake(_userImgView.right+20*PERCEN, _userImgView.top-(WIDTH_5S ? 2 : 0), nickWidth, 20);
    
    _pinlun.frame = CGRectMake(_nickNameLb.right+10*PERCEN, _nickNameLb.top, 80, 20);
    
    _timeLb.frame = CGRectMake(_nickNameLb.left, _nickNameLb.bottom-(WIDTH_5S ? 4:2), 120, 20);
    
    _commentLb.frame = CGRectMake(_userImgView.left, _userImgView.bottom+12*PERCEN, _bgView.width-50*PERCEN-(ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):0), 20);
    //内容
    _contentView.frame = CGRectMake((ISIPAD?480*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):50*PERCEN),_commentLb.bottom+10*PERCEN, (ISIPAD?_bgView.width-960*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):_bgView.width-100*PERCEN), 80*PERCEN);
    if ([_dataDic.allKeys containsObject:@"CommentType"]) {
        if (![[_dataDic objectForKey:@"CommentType"] isKindOfClass:[NSNull class]]) {
            if ([[_dataDic objectForKey:@"CommentType"]intValue] == 1) {
                //亲子秀
                _contentImgView.frame = CGRectMake(0, 0, 80*PERCEN, _contentView.height);
                _contentNameLb.frame = CGRectMake(_contentImgView.right+40*PERCEN, (_contentView.height-20)/2., _contentView.width-_contentImgView.width-50*PERCEN, 20);
            }
            else if ([[_dataDic objectForKey:@"CommentType"]intValue] == 2) {
                //帖子
                _contentNameLb.frame = CGRectMake((ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (_contentView.height-20)/2., _contentView.width-_contentImgView.width-50*PERCEN, 20);
            }
        }
    }
    

    _replyLb.frame = (_replyLb.hidden == YES ? CGRectZero : CGRectMake(_commentLb.left, _contentView.bottom+5, _commentLb.width, 20));
    
    _replyBt.frame = CGRectMake(_bgView.width-80-(ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):20*PERCEN), _timeLb.top-10, 80, 40);
}

@end
